home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / segal / c_array.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-23  |  18.7 KB  |  843 lines

  1.  
  2. /*
  3.  *     c_array.c   a collection of routines for handling
  4.  *                 dynamically allocated arrays in C.  
  5.  *
  6.  *     Brian Tierney  LBL
  7.  *
  8.  *  This routines allocate and access array by creating arrays of pointers.
  9.  *   The reason for these routines are speed and code readability. Allocating
  10.  *   arrays in this manner means that elements can be accessed by pointers
  11.  *   instead of by a multiplication and an addition. This method uses more 
  12.  *   memory, but on a machine with plenty of memory the increase in speed 
  13.  *   is worth it.  
  14.  *
  15.  *    for example, this:
  16.  * 
  17.  *       for (r = 0; r < nrow; r++)
  18.  *           for (c = 0; c < ncol; c++) {
  19.  *            pixel = image[r][c];
  20.  *
  21.  * is faster and more readable (in my opinion), than this:
  22.  *
  23.  *       for (r = 0; r < nrow; r++)
  24.  *           for (c = 0; c < ncol; c++) {
  25.  *            pixel = image[r * nrow + c];
  26.  */
  27.  
  28. /* routines in this file:
  29.  
  30.       alloc_3d_byte_array(nx,ny,nz)
  31.       alloc_2d_byte_array(nx,ny)
  32.       alloc_2d_bit_array(nx,ny) * added by Bryan Skene *
  33.       read_3d_byte_array(fp,array,nx,ny,nz)
  34.       read_2d_byte_array(fp,array,nx ,ny)
  35.       read_2d_bit_array(fp,array,nx ,ny) * added by Bryan Skene *
  36.       write_3d_byte_array(fp,array,nx,ny,nz)
  37.       write_2d_byte_array(fp,array,nx,ny)
  38.       free_3d_byte_array(array)
  39.       free_2d_byte_array(array)
  40.  
  41.   ** and same routines for short, int, and float
  42.  
  43.   *
  44.   *  all read/write routines return a 0 if successful and 1 otherwise 
  45.   *
  46.   *
  47.   *  sample use with hips images:  r=rows, c=cols, f = frames
  48.   *    2D case:
  49.   *    u_char **pic;
  50.   *    pic = alloc_2d_byte_array(r,c);
  51.   *    read_2d_byte_array(stdin, pic1, r,c);
  52.   *
  53.   *    for (i = 0; i < r; i++)
  54.   *    for (j = 0; j < c; j++)  {  * want column to vary fastest *
  55.   *        val = pic[i][j]; 
  56.   *    }
  57.   *    write_2d_byte_array(stdout, pic2, r,c); 
  58.   *
  59.   ****************************************************
  60.   *   3D case:
  61.   *    u_char ***pic;
  62.   *    pic = alloc_3d_byte_array(f,r,c);
  63.   *  
  64.   *  read_3d_byte_array(stdin, pic, f,r,c);
  65.   *    
  66.   *   for (i = 0; i < f; i++)
  67.   *      for (j = 0; j < r; j++)  
  68.   *        for(k=0; k< c; k++) {  * vary col fastest, frame slowest *
  69.   *            val = pic[i][j][k]; 
  70.   */
  71.  
  72. /***********************************************************************/
  73. /*  COPYRIGHT NOTICE         *******************************************/
  74. /***********************************************************************/
  75.  
  76. /*   This program is copyright (C) 1990, Regents  of  the
  77. University  of  California.   Anyone may reproduce this software,
  78. in whole or in part, provided that:
  79. (1)  Any copy  or  redistribution  must  show  the
  80.      Regents  of  the  University of California, through its
  81.      Lawrence Berkeley Laboratory, as the source,  and  must
  82.      include this notice;
  83. (2)  Any use of this software must reference this  distribu-
  84.      tion,  state that the software copyright is held by the
  85.      Regents of the University of California, and  that  the
  86.      software is used by their permission.
  87.  
  88.      It is acknowledged that the U.S. Government has  rights
  89. to this software under  Contract DE-AC03-765F00098 between the U.S.
  90. Department of Energy and the University of California.
  91.  
  92.      This software is provided as a professional  academic
  93. contribution for  joint exchange.  Thus it is experimental, is
  94. provided ``as is'', with no warranties of any kind  whatsoever,
  95. no  support,  promise  of updates, or printed documentation.
  96. Bug reports or fixes may be sent to the author, who may or may
  97. not act on them as he desires.
  98. */
  99.  
  100. /*   Author:  Brian L. Tierney
  101.  *            Lawrence Berkeley Laboratory
  102.  *            Imaging and Distributed Computing Group
  103.  *            email: bltierney@lbl.gov
  104. */
  105.  
  106. #include <stdio.h>
  107. #include <sys/types.h>
  108.  
  109. #define Calloc(x,y) (y *)calloc((unsigned)(x), sizeof(y))
  110. #define Fread(a,b,c,d) fread((char *)(a), b, (int)(c), d)
  111. #define Fwrite(a,b,c,d) fwrite((char *)(a), b, (int)(c), d)
  112.  
  113. /*********************************/
  114. u_char ***
  115. alloc_3d_byte_array(nx,ny,nz)  /* in hips terminology: col,row,frame */
  116. int nx,ny,nz;
  117. {
  118.     u_char ***array;
  119.     register int i, j;
  120.  
  121.     /* allocate 3-d array for input image data */
  122.  
  123.     /* allocate 2 arrays of pointers */
  124.     if ((array = Calloc(nx, u_char **)) == NULL)
  125.     perror("calloc error: array ");
  126.     if ((array[0] = Calloc(nx * ny, u_char *)) == NULL)
  127.     perror("calloc error: array ");
  128.  
  129.     /* allocate array for data */
  130.     if ((array[0][0] = Calloc(nx * ny * nz, u_char)) ==    NULL)
  131.     perror("calloc error: array ");
  132.  
  133.     /* initialize pointer arrays */
  134.     for (i = 1; i < ny; i++)
  135.     array[0][i] = **array + (nz * i);
  136.     for (i = 1; i < nx; i++) {
  137.     array[i] = *array + (ny * i);
  138.     array[i][0] = **array + (nz * ny * i);
  139.     for (j = 1; j < ny; j++)/* initialize pointer array */
  140.         array[i][j] = array[i][0] + (nz * j);
  141.     }
  142.     return(array);
  143. }
  144.  
  145. /**********************************/
  146. u_char **
  147. alloc_2d_byte_array(nx,ny)
  148. int nx,ny;
  149. {
  150.     u_char **array;
  151.     register int i;
  152.  
  153.     /* allocate 2-d array for input image data */
  154.     /* allocate array of pointers */
  155.     if ((array = Calloc(nx, u_char *)) == NULL)
  156.     perror("calloc error: array ");
  157.  
  158.     /* allocate array for data */
  159.     if ((array[0] = Calloc(nx * ny, u_char)) == NULL)
  160.     perror("calloc error: array ");
  161.  
  162.     /* initialize pointer arrays */
  163.     for (i = 1; i < nx; i++) 
  164.     array[i] = array[0] + (ny * i);
  165.  
  166.     return(array);
  167. }
  168.  
  169. /**********************************/
  170. u_char **
  171. alloc_2d_bit_array(nx,ny)
  172. int nx,ny;
  173. {
  174.     u_char **array;
  175.     register int i;
  176.  
  177.     /* allocate 2-d array for input mask data */
  178.     /* allocate array of pointers */
  179.     if ((array = Calloc(nx/8 + 1, u_char *)) == NULL)
  180.     perror("calloc error: array ");
  181.  
  182.     /* allocate array for data */
  183.     if ((array[0] = Calloc((nx/8 + 1) * ny, u_char)) == NULL)
  184.     perror("calloc error: array ");
  185.  
  186.     /* initialize pointer arrays */
  187.     for (i = 1; i < nx/8 + 1; i++) 
  188.     array[i] = array[0] + (ny * i);
  189.  
  190.     return(array);
  191. }
  192.  
  193. /********************************/
  194. int
  195. read_3d_byte_array(fp,array,nx,ny,nz)
  196. FILE *fp;
  197. u_char ***array;
  198. int nx,ny,nz;
  199. {
  200.     long      rsize;
  201.  
  202.     rsize = nx * ny * nz; 
  203.     if (Fread(array[0][0], sizeof(u_char), rsize, fp)  != rsize) {
  204.     perror("\n error reading file\n");
  205.     return(-1);
  206.     }
  207.     return(0);
  208. }
  209.  
  210. /********************************/
  211. int
  212. read_2d_byte_array(fp,array,nx ,ny)
  213. FILE *fp;
  214. u_char **array;
  215. int nx,ny;
  216. {
  217.     long      rsize;
  218.  
  219.     rsize = nx * ny; 
  220.  
  221.     if (Fread(array[0], sizeof(u_char), rsize, fp)  != rsize) {
  222.         perror("\n error reading file\n"); 
  223.         return (-1);
  224.     } 
  225.  
  226.     return(0);
  227. }
  228.  
  229. /********************************/
  230. int
  231. read_2d_bit_array(fp,array,nx ,ny)
  232. FILE *fp;
  233. u_char **array;
  234. int nx,ny;
  235. {
  236.     long      rsize;
  237.  
  238.     rsize = (nx/8 + 1) * ny; 
  239.  
  240.     if (Fread(array[0], sizeof(u_char), rsize, fp)  != rsize) {
  241.         perror("\n error reading file\n"); 
  242.         return (-1);
  243.     } 
  244.  
  245.     return(0);
  246. }
  247.  
  248. /*******************************/
  249. int
  250. write_3d_byte_array(fp,array,nx,ny,nz)
  251. FILE *fp;
  252. u_char ***array;
  253. int nx,ny,nz;
  254. {
  255.     long      size;
  256.  
  257.     size = nx * ny * nz;  
  258.     if (Fwrite(array[0][0], sizeof(u_char), size, fp)  != size) {
  259.     perror("\n error writing file\n");
  260.     return (-1);
  261.     }
  262.  
  263.     return(0);
  264. }
  265.  
  266. /********************************/
  267. int
  268. write_2d_byte_array(fp,array,nx ,ny)
  269. FILE *fp;
  270. u_char **array;
  271. int nx,ny;
  272. {
  273.     long      size;
  274.  
  275.     size = nx* ny; 
  276.  
  277.     if (Fwrite(array[0], sizeof(u_char), size, fp)  != size) {
  278.     perror("\n error writing file\n");
  279.     return(-1);
  280.     }
  281.     return(0);
  282. }
  283.  
  284. /********************************/
  285. int
  286. free_3d_byte_array(array)
  287. u_char ***array;
  288. {
  289.     cfree((char *)array[0][0]);
  290.     cfree((char *)array[0]);
  291.     cfree((char *)array);
  292. }
  293.  
  294. /*********************************/
  295. int
  296. free_2d_byte_array(array)
  297. u_char **array;
  298. {
  299.     cfree((char *)array[0]);
  300.     cfree((char *)array);
  301. }
  302.  
  303. /********************************************************/
  304. /* same routines for data type short                    */
  305. /********************************************************/
  306.  
  307. short ***
  308. alloc_3d_short_array(nx,ny,nz)
  309. int nx,ny,nz;
  310. {
  311.     short ***array;
  312.     register int i, j;
  313.  
  314.     /* allocate 3-d array for input image data */
  315.  
  316.     /* allocate 2 arrays of pointers */
  317.     if ((array = Calloc(nx, short **)) == NULL)
  318.     perror("calloc error: array ");
  319.     if ((array[0] = Calloc(nx * ny, short *)) == NULL)
  320.     perror("calloc error: array ");
  321.  
  322.     /* allocate array for data */
  323.     if ((array[0][0] = Calloc(nx * ny * nz, short)) ==    NULL)
  324.     perror("calloc error: array ");
  325.  
  326.     /* initialize pointer arrays */
  327.     for (i = 1; i < ny; i++)
  328.     array[0][i] = **array + (nz * i);
  329.     for (i = 1; i < nx; i++) {
  330.     array[i] = *array + (ny * i);
  331.     array[i][0] = **array + (ny * nz * i);
  332.     for (j = 1; j < ny; j++)/* initialize pointer array */
  333.         array[i][j] = array[i][0] + (nz * j);
  334.     }
  335.     return(array);
  336. }
  337.  
  338. /**********************************/
  339. short **
  340. alloc_2d_short_array(nx,ny)
  341. int nx,ny;
  342. {
  343.     short **array;
  344.     register int i;
  345.  
  346.     /* allocate 2-d array for input image data */
  347.     /* allocate array of pointers */
  348.     if ((array = Calloc(nx, short *)) == NULL)
  349.     perror("calloc error: array ");
  350.  
  351.     /* allocate array for data */
  352.     if ((array[0] = Calloc(nx * ny, short)) == NULL)
  353.     perror("calloc error: array ");
  354.  
  355.     /* initialize pointer arrays */
  356.     for (i = 0; i < nx; i++) 
  357.     array[i] = *array + (ny * i);
  358.  
  359.     return(array);
  360. }
  361.  
  362. /********************************/
  363. int
  364. read_3d_short_array(fp,array,nx,ny,nz)
  365. FILE *fp;
  366. short ***array;
  367. int nx,ny,nz;
  368. {
  369.     long      rsize;
  370.  
  371.     rsize = nx * ny * nz; 
  372.     if (Fread(array[0][0], sizeof(short), rsize, fp)  != rsize) {
  373.     perror("\n error reading file\n");
  374.     return(-1);
  375.     }
  376.     return(0);
  377. }
  378.  
  379. /********************************/
  380. int
  381. read_2d_short_array(fp,array,nx ,ny)
  382. FILE *fp;
  383. short **array;
  384. int nx,ny;
  385. {
  386.     long      rsize;
  387.  
  388.     rsize = nx * ny; 
  389.     if (Fread(array[0], sizeof(short), rsize, fp)  != rsize) {
  390.         perror("\n error reading file\n");
  391.         return(-1);
  392.     }
  393.     return(0);
  394. }
  395.  
  396. /*******************************/
  397. int
  398. write_3d_short_array(fp,array,nx,ny,nz)
  399. FILE *fp;
  400. short ***array;
  401. int nx,ny,nz;
  402. {
  403.     long      size;
  404.  
  405.     size = nx * ny * nz;  
  406.     if (Fwrite(array[0][0], sizeof(short), size, fp)  != size) {
  407.     perror("\n error writing file\n");
  408.     return(-1);
  409.     }
  410.     return(0);
  411. }
  412.  
  413. /********************************/
  414. int
  415. write_2d_short_array(fp,array,nx ,ny)
  416. FILE *fp;
  417. short **array;
  418. int nx,ny;
  419. {
  420.     long      size;
  421.  
  422.     size = nx* ny; 
  423.  
  424.     if (Fwrite(array[0], sizeof(short), size, fp)  != size) {
  425.     perror("\n error writing file\n");
  426.     return(-1);
  427.     }
  428.     return(0);
  429. }
  430.  
  431. /********************************/
  432. int
  433. free_3d_short_array(array)
  434. short ***array;
  435. {
  436.     cfree((char *)array[0][0]);
  437.     cfree((char *)array[0]);
  438.     cfree((char *)array);
  439. }
  440.  
  441. /*********************************/
  442. int
  443. free_2d_short_array(array)
  444. short **array;
  445. {
  446.     cfree((char *)array[0]);
  447.     cfree((char *)array);
  448. }
  449.  
  450. /****************************************************/
  451. /*  int routines */
  452. /**************************************************/
  453.  
  454. int ***
  455. alloc_3d_int_array(nx,ny,nz)
  456. int nx,ny,nz;
  457. {
  458.     int ***array;
  459.     register int i, j;
  460.  
  461.     /* allocate 3-d array for input image data */
  462.  
  463.     /* allocate 2 arrays of pointers */
  464.     if ((array = Calloc(nx, int **)) == NULL)
  465.     perror("calloc error: array ");
  466.     if ((array[0] = Calloc(nx * ny, int *)) == NULL)
  467.     perror("calloc error: array ");
  468.  
  469.     /* allocate array for data */
  470.     if ((array[0][0] = Calloc(nx * ny * nz, int)) ==    NULL)
  471.     perror("calloc error: array ");
  472.  
  473.     /* initialize pointer arrays */
  474.     for (i = 1; i < ny; i++)
  475.     array[0][i] = **array + (nz * i);
  476.     for (i = 1; i < nx; i++) {
  477.     array[i] = *array + (ny * i);
  478.     array[i][0] = **array + (ny * nz * i);
  479.     for (j = 1; j < ny; j++)/* initialize pointer array */
  480.         array[i][j] = array[i][0] + (nz * j);
  481.     }
  482.     return(array);
  483. }
  484.  
  485. /**********************************/
  486. int **
  487. alloc_2d_int_array(nx,ny)
  488. int nx,ny;
  489. {
  490.     int **array;
  491.     register int i;
  492.  
  493.     /* allocate 2-d array for input image data */
  494.     /* allocate array of pointers */
  495.     if ((array = Calloc(nx, int *)) == NULL)
  496.     perror("calloc error: array ");
  497.  
  498.     /* allocate array for data */
  499.     if ((array[0] = Calloc(nx * ny, int)) == NULL)
  500.     perror("calloc error: array ");
  501.  
  502.     /* initialize pointer arrays */
  503.     for (i = 0; i < nx; i++) 
  504.     array[i] = *array + (ny * i);
  505.  
  506.     return(array);
  507. }
  508.  
  509. /********************************/
  510. int
  511. read_3d_int_array(fp,array,nx,ny,nz)
  512. FILE *fp;
  513. int ***array;
  514. int nx,ny,nz;
  515. {
  516.     long      rsize;
  517.  
  518.     rsize = nx * ny * nz; 
  519.     if (Fread(array[0][0], sizeof(int), rsize, fp)  != rsize) {
  520.     perror("\n error reading file\n");
  521.     return(-1);
  522.     }
  523.     return(0);
  524. }
  525.  
  526. /********************************/
  527. int
  528. read_2d_int_array(fp,array,nx ,ny)
  529. FILE *fp;
  530. int **array;
  531. int nx,ny;
  532. {
  533.     long      rsize;
  534.  
  535.     rsize = nx * ny; 
  536.     if (Fread(array[0], sizeof(int), rsize, fp)  != rsize) {
  537.         perror("\n error reading file\n");
  538.         return(-1);
  539.     }
  540.     return(0);
  541. }
  542.  
  543. /*******************************/
  544. int
  545. write_3d_int_array(fp,array,nx,ny,nz)
  546. FILE *fp;
  547. int ***array;
  548. int nx,ny,nz;
  549. {
  550.     long      size;
  551.  
  552.     size = nx * ny * nz;  
  553.     if (Fwrite(array[0][0], sizeof(int), size, fp)  != size) {
  554.     perror("\n error writing file\n");
  555.     return(-1);
  556.     }
  557.     return(0);
  558. }
  559.  
  560. /********************************/
  561. int
  562. write_2d_int_array(fp,array,nx ,ny)
  563. FILE *fp;
  564. int **array;
  565. int nx,ny;
  566. {
  567.     long      size;
  568.  
  569.     size = nx* ny; 
  570.  
  571.     if (Fwrite(array[0], sizeof(int), size, fp)  != size) {
  572.     perror("\n error writing file\n");
  573.     return(-1);
  574.     }
  575.     return(0);
  576. }
  577.  
  578. /********************************/
  579. int
  580. free_3d_int_array(array)
  581. int ***array;
  582. {
  583.     cfree((char *)array[0][0]);
  584.     cfree((char *)array[0]);
  585.     cfree((char *)array);
  586. }
  587.  
  588. /*********************************/
  589. int
  590. free_2d_int_array(array)
  591. int **array;
  592. {
  593.     cfree((char *)array[0]);
  594.     cfree((char *)array);
  595. }
  596.  
  597. /****************************************************/
  598. /*  float routines */
  599. /**************************************************/
  600.  
  601. float ***
  602. alloc_3d_float_array(nx,ny,nz)
  603. int nx,ny,nz;
  604. {
  605.     float ***array;
  606.     register int i, j;
  607.  
  608.     /* allocate 3-d array for input image data */
  609.  
  610.     /* allocate 2 arrays of pointers */
  611.     if ((array = Calloc(nx, float **)) == NULL)
  612.     perror("calloc error: array ");
  613.     if ((array[0] = Calloc(nx * ny, float *)) == NULL)
  614.     perror("calloc error: array ");
  615.  
  616.     /* allocate array for data */
  617.     if ((array[0][0] = Calloc(nx * ny * nz, float)) ==    NULL)
  618.     perror("calloc error: array ");
  619.  
  620.     /* initialize pointer arrays */
  621.     for (i = 1; i < ny; i++)
  622.     array[0][i] = **array + (nz * i);
  623.     for (i = 1; i < nx; i++) {
  624.     array[i] = *array + (ny * i);
  625.     array[i][0] = **array + (ny * nz * i);
  626.     for (j = 1; j < ny; j++)/* initialize pointer array */
  627.         array[i][j] = array[i][0] + (nz * j);
  628.     }
  629.     return(array);
  630. }
  631.  
  632. /**********************************/
  633. float **
  634. alloc_2d_float_array(nx,ny)
  635. int nx,ny;
  636. {
  637.     float **array;
  638.     register int i;
  639.  
  640.     /* allocate 2-d array for input image data */
  641.     /* allocate array of pointers */
  642.     if ((array = Calloc(nx, float *)) == NULL)
  643.     perror("calloc error: array ");
  644.  
  645.     /* allocate array for data */
  646.     if ((array[0] = Calloc(nx * ny, float)) == NULL)
  647.     perror("calloc error: array ");
  648.  
  649.     /* initialize pointer arrays */
  650.     for (i = 0; i < nx; i++) 
  651.     array[i] = *array + (ny * i);
  652.  
  653.     return(array);
  654. }
  655.  
  656. /********************************/
  657. int
  658. read_3d_float_array(fp,array,nx,ny,nz)
  659. FILE *fp;
  660. float ***array;
  661. int nx,ny,nz;
  662. {
  663.     long      rsize;
  664.  
  665.     rsize = nx * ny * nz; 
  666.     if (Fread(array[0][0], sizeof(float), rsize, fp)  != rsize) {
  667.     perror("\n error reading file\n");
  668.     return(-1);
  669.     }
  670.     return(0);
  671. }
  672.  
  673. /********************************/
  674. int
  675. read_2d_float_array(fp,array,nx ,ny)
  676. FILE *fp;
  677. float **array;
  678. int nx,ny;
  679. {
  680.     long      rsize;
  681.  
  682.     rsize = nx * ny; 
  683.     if (Fread(array[0], sizeof(float), rsize, fp)  != rsize) {
  684.         perror("\n error reading file\n");
  685.         return(-1);
  686.     }
  687.     return(0);
  688. }
  689.  
  690. /*******************************/
  691. int
  692. write_3d_float_array(fp,array,nx,ny,nz)
  693. FILE *fp;
  694. float ***array;
  695. int nx,ny,nz;
  696. {
  697.     long      size;
  698.  
  699.     size = nx * ny * nz;  
  700.     if (Fwrite(array[0][0], sizeof(float), size, fp)  != size) {
  701.     perror("\n error writing file\n");
  702.     return(-1);
  703.     }
  704.     return(0);
  705. }
  706.  
  707. /********************************/
  708. int
  709. write_2d_float_array(fp,array,nx ,ny)
  710. FILE *fp;
  711. float **array;
  712. int nx,ny;
  713. {
  714.     long      size;
  715.  
  716.     size = nx* ny; 
  717.  
  718.     if (Fwrite(array[0], sizeof(float), size, fp)  != size) {
  719.     perror("\n error writing file\n");
  720.     return(-1);
  721.     }
  722.     return(0);
  723. }
  724.  
  725. /********************************/
  726. int
  727. free_3d_float_array(array)
  728. float ***array;
  729. {
  730.     cfree((char *)array[0][0]);
  731.     cfree((char *)array[0]);
  732.     cfree((char *)array);
  733. }
  734.  
  735. /*********************************/
  736. int
  737. free_2d_float_array(array)
  738. float **array;
  739. {
  740.     cfree((char *)array[0]);
  741.     cfree((char *)array);
  742. }
  743.  
  744. /**************************************************************/
  745. /*                  long routines                             */
  746. /**************************************************************/
  747.  
  748. long ***
  749. alloc_3d_long_array(nx,ny,nz)  /* in hips terminology: col,row,frame */
  750. int nx,ny,nz;
  751. {
  752.     long ***array;
  753.     register int i, j;
  754.  
  755.     /* allocate 3-d array for input image data */
  756.  
  757.     /* allocate 2 arrays of pointers */
  758.     if ((array = Calloc(nx, long **)) == NULL)
  759.     perror("calloc error: array ");
  760.     if ((array[0] = Calloc(nx * ny, long *)) == NULL)
  761.     perror("calloc error: array ");
  762.  
  763.     /* allocate array for data */
  764.     if ((array[0][0] = Calloc(nx * ny * nz, long)) ==    NULL)
  765.     perror("calloc error: array ");
  766.  
  767.     /* initialize pointer arrays */
  768.     for (i = 1; i < ny; i++)
  769.     array[0][i] = **array + (nz * i);
  770.     for (i = 1; i < nx; i++) {
  771.     array[i] = *array + (ny * i);
  772.     array[i][0] = **array + (nz * ny * i);
  773.     for (j = 1; j < ny; j++)/* initialize pointer array */
  774.         array[i][j] = array[i][0] + (nz * j);
  775.     }
  776.     return(array);
  777. }
  778.  
  779. /**********************************/
  780. long **
  781. alloc_2d_long_array(nx,ny)
  782. int nx,ny;
  783. {
  784.     long **array;
  785.     register int i;
  786.  
  787.     /* allocate 2-d array for input image data */
  788.     /* allocate array of pointers */
  789.     if ((array = Calloc(nx, long *)) == NULL)
  790.     perror("calloc error: array ");
  791.  
  792.     /* allocate array for data */
  793.     if ((array[0] = Calloc(nx * ny, long)) == NULL)
  794.     perror("calloc error: array ");
  795.  
  796.     /* initialize pointer arrays */
  797.     for (i = 0; i < nx; i++) 
  798.     array[i] = *array + (ny * i);
  799.  
  800.     return(array);
  801. }
  802.  
  803. /********************************/
  804. int
  805. free_3d_long_array(array)
  806. long ***array;
  807. {
  808.     cfree((char *)array[0][0]);
  809.     cfree((char *)array[0]);
  810.     cfree((char *)array);
  811. }
  812.  
  813. /*********************************/
  814. int
  815. free_2d_long_array(array)
  816. long **array;
  817. {
  818.     cfree((char *)array[0]);
  819.     cfree((char *)array);
  820. }
  821.  
  822. /***********************************************************************/
  823. /*  all types: all use these if array is cast when calling the routine */
  824. /***********************************************************************/
  825. int
  826. free_3d_array(array)
  827. char ***array;
  828. {
  829.     cfree(array[0][0]);
  830.     cfree(array[0]);
  831.     cfree(array);
  832. }
  833.  
  834. /*********************************/
  835. int
  836. free_2d_array(array)
  837. char **array;
  838. {
  839.     cfree(array[0]);
  840.     cfree(array);
  841. }
  842.  
  843.